package com.ruoyi.publisher;


import com.ruoyi.config.RabbitMQConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author hlf
 * @createTime 2025/2/6 16:41
 * @description
 */
@Service
public class RabbitMQProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String message) {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setDeliveryMode(MessageProperties.DEFAULT_DELIVERY_MODE.PERSISTENT); // 持久化消息
        Message amqpMessage = new Message(message.getBytes(), messageProperties);
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY, amqpMessage);
        System.out.println("Sent persistent message: " + message);
    }

    public void sendArticleMessage(String message) {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setDeliveryMode(MessageProperties.DEFAULT_DELIVERY_MODE.PERSISTENT); // 持久化消息
        Message amqpMessage = new Message(message.getBytes(), messageProperties);
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY_ARTICLE, amqpMessage);
        System.out.println("Sent persistent message: " + message);
    }


    public void sendArticleMessageAll(String message) {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setDeliveryMode(MessageProperties.DEFAULT_DELIVERY_MODE.PERSISTENT); // 持久化消息
        Message amqpMessage = new Message(message.getBytes(), messageProperties);
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY_ARTICLE_ALL, amqpMessage);
        System.out.println("Sent persistent message: " + message);
    }

    public void sendArticleMessageDel(String message) {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setDeliveryMode(MessageProperties.DEFAULT_DELIVERY_MODE.PERSISTENT); // 持久化消息
        Message amqpMessage = new Message(message.getBytes(), messageProperties);
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY_ARTICLE_DEL, amqpMessage);
        System.out.println("Sent persistent message: " + message);
    }



}
